package com.fangs.yehwangglobal.base;

import static android.app.Notification.VISIBILITY_SECRET;

import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.webkit.WebView;

import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.multidex.MultiDex;

import com.fangs.yehwangglobal.R;
import com.fangs.yehwangglobal.net.NetworkTools;
import com.fangs.yehwangglobal.utils.DisplayUtils;
import com.fangs.yehwangglobal.utils.ToastUtils;
import com.fangs.yehwangglobal.widget.storehouse.StoreHouseHeader;
import com.hjq.language.MultiLanguages;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.FalsifyFooter;

/**
 * Created by Administrator on
 */

public class YehApp extends Application {

    private static YehApp yehApp;
    private NotificationManager notificationManager;
    public static Boolean isDebug = true;
    private AccountService accountService;

    //static 代码段可以防止内存泄露
    static {
        //启用矢量图兼容
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            layout.setPrimaryColorsId(R.color.color_f8f8f8, R.color.black);//全局设置主题颜色
            StoreHouseHeader houseHeader = new StoreHouseHeader(context);
            houseHeader.initWithString(context.getString(R.string.app_name));
            houseHeader.setLineWidth(DisplayUtils.dip2px(context, 2));
            return houseHeader;
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
//            layout.setEnableFooterTranslationContent(false);
            return new FalsifyFooter(context);
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        yehApp = this;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { //Android 9.0 WebView多进程问题
            String processName = getProcessName();
            if (!getPackageName().equals(processName)) {
                WebView.setDataDirectorySuffix(processName);
            }
        }

        NetworkTools.initNetwork();

        MultiDex.install(this);

        initNotificationManager();
    }

    /**
     * 8.0后 通知需要设置渠道
     */
    private void initNotificationManager() {
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("yehwang_notify_channel", "yehwang_notify",
                    NotificationManager.IMPORTANCE_HIGH);
            //是否绕过请勿打扰模式
            channel.canBypassDnd();
            //闪光灯
            channel.enableLights(true);
            //锁屏显示通知
            channel.setLockscreenVisibility(VISIBILITY_SECRET);
            //闪关灯的灯光颜色
            channel.setLightColor(Color.RED);
            //桌面launcher的消息角标
            channel.canShowBadge();
            //是否允许震动
            channel.enableVibration(true);

            AudioAttributes att = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();
//            Uri uri = Uri.parse("android.resource://com.yewang.yehwangmall/raw/" + R.raw.yehwang_sound);
            try {
                Uri uri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);
                //设置自定义通知铃声
                channel.setSound(uri, att);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //设置可绕过  请勿打扰模式
            channel.setBypassDnd(true);
            //设置震动模式
            channel.setVibrationPattern(new long[]{100, 100, 200});
            //是否会有灯光
            channel.shouldShowLights();
            notificationManager.createNotificationChannel(channel);
        }
    }

    public static NotificationManager getNotificationManager() {
        return yehApp.notificationManager;
    }

    public AccountService accountService() {
        if (accountService == null) {
            accountService = new AccountService(getApplicationContext());
        }
        return accountService;
    }

    public static void showToastL(String msg) {
        if (TextUtils.isEmpty(msg)) return;
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        ToastUtils.showCustomLong(R.layout.layout_app_toast, R.id.tv_toast, msg);
    }

    public static void showToastL(@StringRes final int resId) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        ToastUtils.showCustomLong(R.layout.layout_app_toast, R.id.tv_toast, resId);
    }

    public static void showToastS(String msg) {
        if (TextUtils.isEmpty(msg)) return;
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        ToastUtils.showCustomShort(R.layout.layout_app_toast, R.id.tv_toast, msg);
    }

    public static void showToastS(@StringRes final int resId) {
        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        ToastUtils.showCustomShort(R.layout.layout_app_toast, R.id.tv_toast, resId);
    }


    public static YehApp getInstance() {
        return yehApp;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(MultiLanguages.attach(base));
        MultiDex.install(base);
    }
}
